package com.xywy.me;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.xywy.R;
import com.xywy.common.BaseActivity;
import com.xywy.common.FragmentMainActivity;
import com.xywy.common.constants.ConstantValue;
import com.xywy.common.constants.ResultCode;
import com.xywy.common.net.AsyncHttpClient;
import com.xywy.common.net.OnResultListenerAdapter;
import com.xywy.common.net.Parser;
import com.xywy.common.util.MLog;
import com.xywy.common.widget.MyProgress;
import com.xywy.common.widget.TitleBar2;
import com.xywy.me.bean.UserKeeper;
import com.xywy.me.widget.ExitPopupWindow;
import com.xywy.start.SexActivity;

/**
 * 我 - 设置 - 账户 - 邮箱地址
 * 
 * @author xiaogd
 * 
 */
public class SetupAcEmailActivity extends BaseActivity implements
		View.OnClickListener {
	private static final String TAG = "SetupAccountActivity";
	private TextView tv_setup_ac_qrxg;
	private String userid;
	private EditText et_setup_ac_email;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 设置为全屏
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.me_setup_ac_email);

		this.preInit();
		this.initView();
		this.loadData();
		// this.setListener();
	}

	public void startOnClick(View view) {
		switch (view.getId()) {
		case R.id.bt_height_next:

			break;
		}
		;
	}

	@Override
	protected void onResume() {
		super.onResume();

	}

	@Override
	protected void onPause() {
		super.onPause();

	}

	@Override
	public void preInit() {
		Intent intent = this.getIntent();
//		userid = intent.getStringExtra("userid");
		userid = UserKeeper.getCurrentUser(this).getUserid();
	}

	@Override
	public void loadData() {

	}

	@Override
	public void initView() {
		// title
		TitleBar2 titleBar = (TitleBar2) findViewById(R.id.me_setup_ac_email_title_bar);
		titleBar.onClickBack(this);
		TextView tvTitle = titleBar.getTitle();
		TextView tvBack = titleBar.getTvBack();
		titleBar.getGo().setVisibility(View.INVISIBLE);
		tvTitle.setText(getString(R.string.me_setup_account_yxdz));
		tvBack.setText(getString(R.string.me_setup_account));
		et_setup_ac_email = (EditText) findViewById(R.id.et_setup_ac_email);
		tv_setup_ac_qrxg = (TextView) findViewById(R.id.tv_setup_ac_qrxg);
		tv_setup_ac_qrxg.setOnClickListener(this);

	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.tv_setup_ac_qrxg:
			if (checkEmail(et_setup_ac_email.getText().toString())) {
				sendMsg();
			} else {
				Toast.makeText(SetupAcEmailActivity.this, "请输入正确的邮箱地址",
						Toast.LENGTH_SHORT).show();
			}

			break;
		}
	}

	public boolean checkEmail(String email) {
		boolean flag = false;
		try {
			String check = "[\\w\\.\\-]+@([\\w\\-]+\\.)+[\\w\\-]+";
			Pattern regex = Pattern.compile(check);
			Matcher matcher = regex.matcher(email);
			flag = matcher.matches();
		} catch (Exception c) {
			return false;
		}
		return flag;

	}

	MyProgress myProgress;
	/**
	 * 返回
	 */
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			MLog.i(TAG, "back:" + msg.obj + "resuletCode:" + msg.arg1);
			myProgress.stop();
			if (msg.arg1 == ResultCode.SUCCESS) {
				Toast.makeText(SetupAcEmailActivity.this,
						getString(R.string.me_updateemail_success),
						Toast.LENGTH_LONG).show();

				// if (null != target &&
				// target.equals(ConstantValue.LOGINVALCODE)) {
				Intent intent = new Intent(SetupAcEmailActivity.this,
						FragmentMainActivity.class);
				startActivity(intent);
				// finish();
				// }
				SetupAcEmailActivity.this.finish();
			} else {
				Toast.makeText(SetupAcEmailActivity.this,
						"修改邮箱失败",
						Toast.LENGTH_LONG).show();
			}
		}
	};

	// 封装Parser参数
	private Parser paramsParser;
	// 封装httpclient参数
	private HashMap<String, Object> params;
	// 异步加载
	private AsyncHttpClient asyncHttpClient;

	// 处理网络请求，修改密碼
	public void sendMsg() {
		myProgress = new MyProgress(this, 10);
		myProgress.start();
		paramsParser = new Parser();

		// 封装参数
		params = new HashMap<String, Object>();

		// 请求路径
		paramsParser.action = getString(R.string.update_user_infor);
		// 请求方式
		paramsParser.request = "post";

		// if (userid.equals("")) {
		// SharedPreferences sp = context.getSharedPreferences("config",
		// Context.MODE_PRIVATE);
		// userid = sp.getString("userid", "");
		// }

		// 请求参数
		params.put("userid", userid);
		params.put("keyword", ConstantValue.EMAIL);
		params.put("value", et_setup_ac_email.getText().toString());
		paramsParser.map = params;
		// 传递上下文
		paramsParser.context = this.context;

		asyncHttpClient = AsyncHttpClient.getInstance();
		asyncHttpClient.exeute(paramsParser, onResultListenerAdapter);
	}

	OnResultListenerAdapter onResultListenerAdapter = new OnResultListenerAdapter() {
		@Override
		public <T extends Parser> void onSuccess(T resultParser, int resultCode) {
			super.onSuccess(resultParser, resultCode);
			Message message = Message.obtain();
			message.arg1 = resultCode;
			message.obj = resultParser.back;
			handler.sendMessage(message);
		}
	};

}
